package com.hu.boot.interceptors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// 自定义拦截器
public class Interceptor2 implements HandlerInterceptor {

    // 声明一个日志对象
    private static final Logger log = LoggerFactory.getLogger(Interceptor2.class);

    // 最先执行方法
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("===== 4 ======");
        // 返回 true 时放行,返回 false 中断
        return true;
    }

    // controller 返回之后回到 postHandler 这个方法执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("===== 5 ======");
    }

    // 相当于 finally {} 代码块中的内容,无论请求是否正确以及是否出现异常,都会执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("===== 6 ======");
    }

}
